<template>

  <el-button type="primary"  plain icon="Plus" @click="handleAdd"
    >新增</el-button
  >
  <el-tooltip class="item" effect="dark" content="该操作将会复制上个月的评分条目以及岗位绩效中的评分条目" placement="top">
  <el-button type="success" plain icon="Check" @click="handleCopy"
    >复制上月</el-button
  >
  </el-tooltip>
  <el-button color="#626aef" :dark="isDark" plain icon="Download" @click="handleUP"
    >导入</el-button
  >
  <!--<el-button color="#626aef" :dark="isDark" plain icon="Download" @click="handleOut"
    >导出</el-button
  >-->
   <el-tooltip class="item" effect="dark" content="刷新" placement="top">
    <el-button circle icon="Refresh" @click="update" />
  </el-tooltip>
  <el-table
    :data="PositionList"
    border
    style="width: 90%; margin: 20px auto;border-radius: 8px;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :cell-style="{ textAlign: 'center' }"
  >
    <el-table-column prop="region" label="地区" width="180"> </el-table-column>
    <el-table-column prop="coefficient" label="地区系数" width="180" />
    <el-table-column
      prop="state"
      label="状态"
      width="82"
      align="center"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.state === 0 ?'danger' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未启用" : "启用" }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column prop="ins" label="备注" />
    <el-table-column prop="updateTime" label="最后更新时间" width="200" />
    <el-table-column label="操作" width="250">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <Dialog v-model="dialogFormVisible" :close="close" :update="update" :selectContent="selectContent" :type="type" :title="title"></Dialog>
  <pagination
    class="pagination"
    v-show="PositionPage.total > 0"
    :total="PositionPage.total"
    v-model:page="PageForm.page"
    v-model:limit="PositionPage.size"
    @pagination="handlePagination"
  />
  <FormDialog
    v-model="dialog"
    :close="closeForm"
    :update="update"
  ></FormDialog>
  <UploadExcel
        v-model="upload.open"
        :title="upload.upLoadTitle"
        :url="importUrl"
        @onSuccess="onSuccess"
        @onDownTemplate="onDownTemplate"
      ></UploadExcel>
</template>
<script setup>
import {ref,onMounted,onBeforeMount,reactive,defineEmits,watch,} from "vue";
import {deleteRegionCoefficient,updateRegionCoefficient,copy,getRegionNowPage} from "@/api/regionCoefficient";
import { ElMessage } from "element-plus";
import Dialog from "./regionDialog";
import FormDialog from "./regionForm";
import UploadExcel from "@/components/SendAndDownload/index";
import {getRegionTemplate} from "@/api/template";

const PageForm = ref({
  page: 1,
  page_size: 10,
});
const PositionPage=ref([])
const PositionList=ref([])
onBeforeMount(() => {
  getRegionNowPage(PageForm.value).then((data) => {
    PositionPage.value=data.data;
    PositionList.value=data.data.records;
  });
});

watch(
  PositionList,
  (newVal) => {
    
  },
  { deep: true }
);

const importUrl = ref('/RegionCoefficient/upload');
const upload = ref({
  open: false,
  upLoadTitle: "地区系数上传",
});
const onSuccess = (res) => {
    getRulePage(value).then(data=>{
        ScoreListPage.value = data.data;
        ScoreRuleList.value = data.data.records;
    })
  upload.value.open = false;
};
const handleUP = () => {
  upload.value.open = true;
};
const onDownTemplate = () => {
  getRegionTemplate();
};

const dialogFormVisible = ref(false);
const title=ref("新增地区系数")
const type=ref("add")
const selectContent=ref()
const handleEdit = (index,row) => {
  selectContent.value = row;
  dialogFormVisible.value = true;
  title.value="编辑地区系数"
  type.value="edit"
};
const handleAdd = (index,row) => {
  selectContent.value={
    id:null,
      region:'',
      coefficient:'',
      state:true,
      ins:''
  }
  dialogFormVisible.value = true;
  title.value="新增地区系数"
  type.value="add"
};
const handleDelete = (index, row) => {
  deleteRegionCoefficient(row).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      update();
    } 
    else {
      ElMessage.error("删除失败");
    }
  });
};

const dialog = ref(false);
const handleCopy=()=>{
  dialog.value = true;
}
const closeForm = () => {
  dialog.value = false;
};

const handlePagination=(value)=>{
    getRegionNowPage(value).then(data=>{
        PositionPage.value=data.data;
        PositionList.value=data.data.records;
    })
}

const close = () => {
  dialogFormVisible.value = false;
};
const update = () => {
  getRegionNowPage(PageForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      PositionPage.value=data.data;
      PositionList.value=data.data.records;
    } 
    else {
      ElMessage.error("更新失败");
    }
  });
};
</script>

<style lang="scss" scoped>
.title1 {
  color: #95d475;
  width: 200px;
  height: 35px;
  font-size: 25px;
  background: rgba(179, 225, 157, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 20px auto;
  line-height: 35px;
}
</style>